/*** REGRESSIONS - DETERMINANTS OF TRACKING ***/

clear all
set more 1
capture log close
set scheme s2mono

global TARGET_TYPES zscore rscore
global EXPRESSION_TYPES rho sigma
global FRAME_TYPES fabs vrel mrel

do "paths.do"
cd "$WORKING"

log using "$LOGFILES/txtr_4.log", replace

/*******************************************************************************
Read and clean data
*******************************************************************************/

use "$WORKING/txtr_2", clear

gen all = 1
gen elem = grade <= 5
gen middle = grade > 5

gen cgenrl0 = log(cgenrl)

** treat charter schools as if they are in geographic districts
gen geo_sample = 1
foreach vv in distnum cntydempa dedge_priv dfvtott denrtot {
    capture drop `vv'0
    gen `vv'0 = `vv'
	replace `vv'0 = geo_`vv' if (cflchart == 1) & (has_geo_data == 1)
	replace geo_sample = 0 if (`vv'0 == .)
}

replace dfvtott0 = log(dfvtott0)
replace denrtot0 = log(denrtot0)

gen lep_con = cgbil_non2way + cgesl_con
gen dis_any = cgdis_phy + cgdis_oth

gen t_unadj = trk_zsco_fa
gen t_rel = trk_zsco_mr

gen curric_diff = 1 - hindex_predrop

** save regression-ready dataset
save "$WORKING/txtr_4a.dta", replace

egen dg_id = group(distnum0 grade)
egen cg_id = group(campus grade)

local depvars t_unadj t_rel
local elemtype all elem middle

tab grade, generate(grade_)
drop grade_1
tab year, generate(year_)
drop year_1

/*******************************************************************************
Enumerate variables and specifications
*******************************************************************************/

// Standard Controls
local stdcntrlsA year_* cgenrl0 ///
		cflhasg5 cflhasg7 denrtot0 dfvtott0 dgsuburb dgtown dgrural ///
		cgmnzlm
local stdcntrlsB year_* cgenrl0 ///
		cflhasg5 cflhasg7 ///
		cgmnzlm
local sdcntrls cgsdzlm
local robcntrls cgnomath cgmisslm
local flexcntrls cgdec10zlm cgdec25zlm cgdec75zlm cgdec90zlm

// Policies
local pol_group cggifted cgdis_phy cgdis_oth cgspedr ///
		cglep lep_con
local pol_qual ctexpa cgclsszavg curric_diff
local pol_1 `pol_group' `sdcntrls'
local pol_2 `pol_group' `sdcntrls' `pol_qual'
local pol_3 `pol_group' `sdcntrls' `pol_qual' `flexcntrls'
local pol_4 `pol_group' `sdcntrls' `pol_qual' `flexcntrls' `robcntrls'
local spec_h1 pol_1 pol_2 pol_3 pol_4

// Determinants
// Hypothesis 1: District
local det_schooltype cflmagnet cflchart
local det_attitude cntydempa0 dedge_priv0
local det_race cghisp cgblack cgasian cgother cglep cgdisadv
local det_1 `det_schooltype'
local det_2 `det_schooltype' `sdcntrls'
local det_3 `det_schooltype' `sdcntrls' `det_attitude'
local det_4 `det_schooltype' `sdcntrls' `det_attitude' `det_race'
local det_5 `det_schooltype' `sdcntrls' `det_attitude' `det_race' `flexcntrls'
local det_6 `det_schooltype' `sdcntrls' `det_attitude' `det_race' `flexcntrls' `robcntrls'
local det_7 `det_race'
local det_8 `det_race' `sdcntrls'
local det_9 `det_race' `sdcntrls' `det_schooltype' `det_attitude'
local det_10 `det_race' `sdcntrls' `det_schooltype' `det_attitude' `flexcntrls'
local spec_h2 det_1 det_2 det_3 det_4 det_5 det_6 det_7 det_8 det_9 det_10

/*******************************************************************************
Regressions
*******************************************************************************/

foreach ctrls in A B {
	local stdcntrls `stdcntrls`ctrls''
	
	forvalues i=1(1)2 {
		estimates clear
		
		capture drop sample0
		gen sample0 = geo_sample
		
		foreach fetype in none dg_id cg_id {
			foreach tt in `depvars' {
				foreach ss in `elemtype' {
					foreach vv in `spec_h`i'' {
						di "$S_DATE $S_TIME `ctrls' `i' `fetype' `tt' `ss' `vv'"
						
						if ("`fetype'" == "none"){
							reg `tt' `stdcntrls' i.grade ``vv'' if (`ss' & sample0) [aw=cgenrl], cl(distnum0)
							eststo, title("`vv' `tt' `ss' fe-`fetype'")
						}
						else {
							reghdfe `tt' `stdcntrls' ``vv'' if (`ss' & sample0) ///
									[aw=cgenrl], absorb(`fetype') vce(cluster distnum0)
							
							eststo, title("`vv' `tt' `ss' fe-`fetype'")
						}
					}
				}
			}
		}
		esttab using "$OUTFILES/txtr_4-h`i'`ctrls'.csv", csv b(%9.3f) varwidth(16) se(%9.3f) r2 ar2 ///
				scalars(N_clust) star(* 0.10 ** 0.05 *** 0.01) mlabels(,titles) replace
	}

}

log close

